﻿using AutoRegistDependency.Attributes;
using AutoRegistDependency.Interface;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyWpf.Bus;
using HandyWpf.ViewModels.Base;
using HandyWpf.Views.Sys.Menu;
using Models.Entities.SystemEntities;
using Models.Queries.SystemQueries;
using Quartz.Util;
using Services.Sys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HandyWpf.ViewModels.Sys
{
    [Component]
    public partial class MenuInfoViewModel : BasePageCRUDViewModel<MenuInfo, MenuQuery, IMenuInfoService, int, MenuForm>
    {
        protected IMenuInfoService menuInfoService;

        public override string DialogToken => "MenuPage";

        public MenuInfoViewModel(IEventBus eventBus, IMenuInfoService menuInfoService) : base(eventBus, menuInfoService)
        {
            this.menuInfoService = menuInfoService;
            GetList();
        }

        public override Expression<Func<MenuInfo, bool>> GetQueryExpression()
        {
            Expressionable<MenuInfo> expressionable = new();
            expressionable = expressionable.AndIF(Query.Id != null, t => t.Id == Query.Id)
                .AndIF(!string.IsNullOrWhiteSpace(Query.Name), t => Query.Name!.Contains(t.Name))
                .AndIF(!string.IsNullOrWhiteSpace(Query.ClassName), t => Query.ClassName!.Contains(t.ClassName));
            return expressionable.ToExpression();
        }

        public override (Expression<Func<MenuInfo, object>>, OrderByType)? GetOrderByExpression()
        {
            Expression<Func<MenuInfo, object>> exp = t => t.Sort;
            var orderByType = OrderByType.Asc;
            return (exp, orderByType);
        }

        public override MenuInfo GetEntityById(int primaryKey)
        {
            return baseService.Find(t => t.Id == primaryKey);
        }
    }
}
